package com.tanhua.admin.listener;

import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.domain.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class LogListener {

    @Autowired(required = false)
    private LogMapper logMapper;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "tanhua.log.queue"),
            exchange = @Exchange(value = "tanhua.log.exchange", type = ExchangeTypes.TOPIC),
            key = {"log.*"}))
    public void saveLog(Map<String, Object> paramMap) {
        log.info("=======================开始保存日志=======================");
        Long userId = Long.valueOf(paramMap.get("userId").toString());
        String type = (String) paramMap.get("type");
        String logTime = (String) paramMap.get("logTime");
        Log log = new Log(userId, logTime, type);
        logMapper.insert(log);
    }
}
